<?php
////////////////////////////////////////////////////////////////////
/*
	---------------------------------------------
	XML Dataset Class
	XMLDataset.class.php
	Eric Hendrickson
	---------------------------------------------
	Revision History
	1.0 - 4/29/2006
	---------------------------------------------
	This class is used to create a connection object on the server-
	side so that asynchronos XML connections can be made from the 
	client-side.  
*/
////////////////////////////////////////////////////////////////////

class XMLDataset {
	//--------------------------------------------------------------------
	// Borrowed Code: 
	// The following functions were acquired from baseXML.php written by
	// Frederico Caldeira Knabben (fredck@fckeditor.net)
	// and incorporated into this file and modified on 4/30/2006 
	// by Eric Hendrickson.
	//-------------------------------------------------------------------
	// Start of Barrowed Code //
	
	function setRequestHeaders() {
		// Prevent the browser from caching the result.
		// Date in the past
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT') ;
		// always modified
		header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT') ;
		// HTTP/1.1
		header('Cache-Control: no-store, no-cache, must-revalidate') ;
		header('Cache-Control: post-check=0, pre-check=0', false) ;
		// HTTP/1.0
		header('Pragma: no-cache') ;
	
		// Set the response format.
		header( 'Content-Type:text/XML; charset=utf-8' ) ;
	}
	
	function createXMLHeader()	{
		$this->setRequestHeaders() ;
		
		// Create the XML document header.
		return '<?xml version="1.0" encoding="utf-8" ?><dataset>' ;
	}
	
	function createXMLFooter() {
		return '</dataset>' ;
	}
	
	// End of Barrowed Code //

	function buildDataset($sql) {
		global $DB;
		$DB->query($sql);
		
		$returnString = '';
		
		$fields = $DB->get_result_fields();
	
		while($row = $DB->fetch_row()) {
			$returnString = $returnString . '<record>';
			for($fieldsCount = 0;$fieldsCount < count($fields);$fieldsCount++) {
				$returnString = $returnString . '<column name="' . $fields[$fieldsCount]->name . '" value="' . htmlentities($row[$fields[$fieldsCount]->name]) . '" />'; 			
			}
			$returnString = $returnString . '</record>'; 			
		}
		
		return $returnString;
	}
	
	function getXMLDataset($sql) {
		return $this->createXMLHeader() . $this->buildDataset($sql) . $this->createXMLFooter();
	}
}
?>